﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace David_Albert
{
    public partial class FormAutor : Form
    {
        public FormAutor()
        {
            InitializeComponent();
        }

        private void btnMPaut_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormAutor_Load(object sender, EventArgs e)
        {
            List<Autor> autors = BaseDeDades.llistarAutor();

            foreach (Autor a in autors)
            {
                int index = dGVAutors.Rows.Add();
                dGVAutors.Rows[index].Cells[0].Value = a.IdAutor;
                dGVAutors.Rows[index].Cells[1].Value = a.Nom;
                dGVAutors.Rows[index].Cells[2].Value = a.Cognom;
            }

        }

        private void btnAgrAut_Click(object sender, EventArgs e)
        {
            FormMantAuto frmaau = new FormMantAuto();
            frmaau.ShowDialog();
            dGVAutors.Rows.Clear();
            object s = new object();
            EventArgs l = new EventArgs();
            FormAutor_Load(s, l);     
        }

        private void btnModificarAutor_Click(object sender, EventArgs e)
        {
            int idA = (int)dGVAutors.CurrentRow.Cells["Identificador"].Value;
            string nom = dGVAutors.CurrentRow.Cells["Nom"].Value.ToString();
            string cognom = dGVAutors.CurrentRow.Cells["Cognoms"].Value.ToString();
            Autor a = new Autor(idA, nom, cognom);

            FormModificarAutor frmModificarAutor = new FormModificarAutor(a);
            frmModificarAutor.ShowDialog();
            dGVAutors.Rows.Clear();
            object s = new object();
            EventArgs l = new EventArgs();
            FormAutor_Load(s, l);   
        }

        private void btnDelAut_Click(object sender, EventArgs e)
        {
            //creo objecta DialogResult per guarda-me la resposa i mira si es si o no
            DialogResult r = new DialogResult();
            r=MessageBox.Show("Vols eliminar "+dGVAutors.CurrentRow.Cells["Nom"].Value.ToString()+"?",
                "Informacio", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                try
                {
                    BaseDeDades.eliminarAutor((int)dGVAutors.CurrentRow.Cells["Identificador"].Value);
                    MessageBox.Show("Autor eliminat correctament", "Informacio", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception n)
                {
                    MessageBox.Show("No s'ha pogut eliminar l'autor", "Informacio", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {

            }
            dGVAutors.Rows.Clear();
            object s = new object();
            EventArgs l = new EventArgs();
            FormAutor_Load(s, l);
        }      
    }
}
